package be.kdg.netpresentadmin;

import java.awt.*;
import java.awt.event.*;
import java.util.ArrayList;
import java.util.Iterator;

import javax.swing.*;

/**
* Special Panel used to show the Questions and answers and to enable
* the admin to answer questions. 
*
* @autor Timothy Huber, Joris Clevenbergs, Rik verschuren, Wim Van der Wilt
*/
public class QnAPanel extends JPanel implements FocusListener, ActionListener,
		QuestionSelectedEventListener {

	/**
	 * Default version ID
	 */
	private static final long serialVersionUID = 1L;

	private ArrayList<QuestionAnsweredEventListener> listeners = new ArrayList<QuestionAnsweredEventListener>();

	private Dimension size;
	private JButton button;
	private JTextArea newQuestion;
	private static int qID = -1;
	private static String Typ = "Typ hier nu uw antwoord op deze vraag";
	private static String Empty = "";
	private static int X = 820;
	private static int Y = 620;

	/**
	* Constructor for QnAPanel.
	* 
	* @param size	dimension of the panel.
	*/
	public QnAPanel(Dimension size) {
		this.size = size;
		initialisePanel();

		button = new JButton("Vraag beantwoorden");
		button.setBounds(X, Y + 98, this.size.width, 20);
		button.setPreferredSize(new Dimension(this.size.width, 25));
		button.addActionListener(this);
		add(button);
	}

	/**
	* Init function that initializes the panel.
	*/
	private void initialisePanel() {
		newQuestion = new JTextArea();
		newQuestion.setLineWrap(true);
		newQuestion.setWrapStyleWord(true);
		newQuestion.setBounds(X, Y, size.width, 108);
		newQuestion.setBackground(Color.white);

		newQuestion.setText(Typ);
		newQuestion.addFocusListener(this);

		newQuestion.setEditable(true);

		JScrollPane textScrollPane = new JScrollPane(newQuestion);
		textScrollPane.setBorder(null);
		textScrollPane
				.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
		textScrollPane
				.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);

		textScrollPane.setPreferredSize(new Dimension(size.width, 105));

		add(textScrollPane);
	}

	/**
	* Overloaded method that handles the FocusEvents when
	* the answer field gains focus.
	* 
	* @param e	FocusEvent that has to be handled.
	*/
	public void focusGained(FocusEvent e) {
		/**
		 * Check if the string contained in the text field equals
		 * the "typ" message, if so, replace it with "empty". 
		 */
		if (newQuestion.getText().equals(Typ)) {
			newQuestion.setText(Empty);
		}
	}

	/**
	* Overloaded method that handles the FocusEvents when
	* the answer field loses focus.
	* 
	* @param e	FocusEvent that has to be handled.
	*/
	public void focusLost(FocusEvent e) {
		/**
		 * Check if the user has typed some meaningful text into the
		 * text field. When focus is lost and the text field is stil empty,
		 * put the status message "typ" back.
		 */
		if (newQuestion.getText().equals(Empty)) {
			newQuestion.setText(Typ);
		}
	}

	/**
	* Overloaded method that handles the ActionEvents when
	* the button is clicked.
	* 
	* @param e	ActionEvent that has to be handled.
	*/
	public void actionPerformed(ActionEvent e) {
		/**
		 * Check what content is available at the point the button is clicked.
		 * If the text differs from "typ" or "empty" it is a -valid- question
		 * and should be handled.
		 */
		if (!(newQuestion.getText().equals(Typ) || newQuestion.getText()
				.equals(Empty))) {
			if (qID != -1) {
				fireQuestionAnsweredEvent(qID, newQuestion.getText());
				qID = -1;
			}
			newQuestion.setText(Typ);
		}
	}

	/**
	* This function adds QuestionAnsweredEventListeners to the listeners list.
	* 
	* @param l	QuestionAnsweredEventListener that needs to be added to the listeners list.
	*/
	public synchronized void addListener(QuestionAnsweredEventListener l) {
		listeners.add(l);
	}

	/**
	* This function removes QuestionAnsweredEventListeners from the listeners list.
	* 
	* @param l	QuestionAnsweredEventListener that needs to be removed from the listeners list.
	*/
	public synchronized void removeListener(QuestionAnsweredEventListener l) {
		listeners.remove(l);
	}

	/**
	* The next method sends QuestionAnsweredEvents to all the listener when a question is asked.
	* 
	* @param id			Integer that represents the id of the answered question.
	* @param question	String that contains the new question that has to be send to the listeners.
	*/
	private synchronized void fireQuestionAnsweredEvent(int id, String question) {
		QuestionAnsweredEvent event = new QuestionAnsweredEvent(this, id,
				question);
		Iterator<QuestionAnsweredEventListener> itListeners = listeners
				.iterator();
		while (itListeners.hasNext()) {
			((QuestionAnsweredEventListener) itListeners.next())
					.questionAnsweredEventRecieved(event);
		}
	}

	/**
	* A function that handles the QuestionSelectedEvents.
	*  
	* @param event	QuestionSelectedEvent that needs to be handled.
	*/
	public void questionSelectedEventRecieved(QuestionSelectedEvent event) {
		qID = event.getQuestionId() - 1;
		newQuestion.setText(Typ);

	}
}